package views.screens.nightScreens;

import controller.human.Person;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.JPanel;
import views.MafiaFrame;
import views.panes.ButtonUser;
import views.panes.ButtonUserDead;
import views.panes.ButtonUserDisabled;
import views.screens.Screen;

public class nightScreen1 extends JPanel implements Screen {

    int nrRows, nrCols;
    MafiaFrame mafiaFrame;
    public ArrayList<String> voted = new ArrayList<String>();

    public nightScreen1(MafiaFrame mf, ArrayList<String> voted) {
        super(new GridLayout(getNrRows(mf.players.size()) + 2, getNrCols(mf.players.size())));
        this.voted = voted;
        init(mf);
    }

    public nightScreen1(MafiaFrame mf) {
        super(new GridLayout(getNrRows(mf.players.size()) + 2, getNrCols(mf.players.size())));
        init(mf);
    }

    private void init(MafiaFrame mf){

        this.mafiaFrame = mf;
        nrRows = getNrRows(mf.players.size()) + 2;
        nrCols = getNrCols(mf.players.size());
        add(new JLabel("Chose yourself: "));
        for (int i = 1; i < nrCols; i++) {
            add(new Component() {
            });
        }

        Iterator<Person> it = mf.players.iterator();
        while (it.hasNext()) {
            Person p = it.next();
            JPanel button = null;
            switch (p.getPhysicalState()) {
                case Dead:
                    button = new ButtonUserDead(p.getName());
                    break;
                default:
                    if (!voted.contains(p.getName())) {
                        button = new ButtonUser(p.getName(), this);
                    } else {
                        button = new ButtonUserDisabled(p.getName());
                    }
            }
            add(button);
        }
    }

    public static int getNrCols(int nrPlayers) {
        if (nrPlayers < 9) {
            return 3;
        }
        if (nrPlayers < 16) {
            return 4;
        }
        return 5;
    }

    public static int getNrRows(int nrPlayers) {
        int nrCols = getNrCols(nrPlayers);
        return nrPlayers / nrCols;
    }

    @Override
    public void setAction(String action) {
        voted.add(action);
        mafiaFrame.update(new nightScreen2(mafiaFrame, action, this));
    }
}
